package org.fanrui.hrm.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.fanrui.hrm.constants.login.LoginConstants;
import org.fanrui.hrm.domain.Login;
import org.fanrui.hrm.mapper.LoginMapper;
import org.fanrui.hrm.service.ILoginService;
import org.fanrui.hrm.service.IMealService;
import org.fanrui.hrm.util.AssertUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * <p>
 * 登录表 服务实现类
 * </p>
 *
 * @author fanrui
 * @since 2024-09-12
 */
@Service
@Transactional(rollbackFor = {Exception.class})
public class LoginServiceImpl extends ServiceImpl<LoginMapper, Login> implements ILoginService {

    @Resource
    private IMealService mealService;

    /**
     * 保存一个login和套餐id 只是后台用
     *
     * @param login  登录信息
     * @param mealId 套餐id
     */
    @Override
    public Login saveLoginAndMealId(Login login, Long mealId) {
        try {

            AssertUtil.notStringEmpty(login.getUsername(), "用户名不可为空");
            AssertUtil.notStringEmpty(login.getPassword(), "密码不可为空");
            AssertUtil.notObjectNull(mealId, "套餐id不可为空");

            // 保存登录信息
            //账户启用
            login.setEnabled(true);
            //标明为后台用户
            login.setType(LoginConstants.LOGIN_TYPE_BACKEND);
            login.setCredentialsNonExpired(true);
            login.setAccountNonExpired(true);
            login.setAccountNonLocked(true);
            baseMapper.insert(login);//保存一个login 密码Oauth2做
            // 保存套餐id
            Long loginId = login.getId();
            mealService.saveRelationWidthLogin(loginId, mealId);
            return login;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
